package com.joke.mydiary.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Bitmap工具类
 * Created by Administrator on 2015/12/7.
 */
public class BitmapUtils {

    /**
     * 得到处理后的Bitmap
     * @param filePath
     * @param reqWidth
     * @param reqHeigth
     * @return
     */
    public static Bitmap decodeSimpledBitmapFromResourse(String filePath, int reqWidth, int reqHeigth) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateInSimpleSize(options, reqWidth, reqHeigth);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);

    }

    /**
     * 用于计算图片采样比例
     *
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private static int calculateInSimpleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        int width = options.outWidth;
        int height = options.outHeight;
        int inSimpleSize = 1;
        if (width > reqWidth || height > reqHeight) {
            if (width > height) {
                inSimpleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSimpleSize = Math.round((float) width / (float) reqWidth);
            }

        }
        return inSimpleSize;

    }

}
